# -*- coding: utf-8 -*-
from waveapi import events
from waveapi import model
from waveapi import robot
from waveapi import document
import logging

def OnParticipantsChanged(properties, context):
  """Invoked when any participants have been added/removed."""
  added = properties['participantsAdded']
  for p in added:
    Notify(context)

def OnRobotAdded(properties, context):
  """Invoked when the robot has been added."""
  root_wavelet = context.GetRootWavelet()
  root_wavelet.CreateBlip().GetDocument().SetText("I'm alive!")

def Notify(context):
  root_wavelet = context.GetRootWavelet()
  root_wavelet.CreateBlip().GetDocument().SetText("Hi everybody!")


def OnBlipSubmitted(properties, context):
  #logging.debug('properties')
  #logging.debug(properties)
  blipId = properties['blipId']
  blip = context.GetBlipById(blipId) #OpBasedBlip
  root_wavelet = context.GetRootWavelet()
  if blip:
    doc = blip.GetDocument() # OpBasedDocument
    text = doc.GetText()
    tempText = text.lower()
    """doc.AppendText('\n Kaddi te hans bol ve! %s \n' % text)"""
    if "who is slim shaddy" in tempText:
      root_wavelet.CreateBlip().GetDocument().SetText("Pranny is slim shaddy! ahun ahun ahun..")
    if "ppimage" in tempText:
      img = document.Image(url="http://testslimbot.appspot.com/images/pp.jpg")
      doc.AppendElement(img)
    if "who is your master" in tempText:
      root_wavelet.CreateBlip().GetDocument().SetText("My master is Sunny Luthra.\n He is one of the laziest person I have ever seen.\n You can follow him on twitter (@SunnyLuthra), though he hardly posts anything overthere. btw he is a nice guy and he loves to help :D.")
    
if __name__ == '__main__':
  myRobot = robot.Robot('testslimbot', 
      image_url='http://testslimbot.appspot.com/images/icon.jpg',
      version='1',
    profile_url='http://testslimbot.appspot.com/')
  myRobot.RegisterHandler(events.BLIP_SUBMITTED, OnBlipSubmitted)
  myRobot.RegisterHandler(events.WAVELET_SELF_ADDED, OnRobotAdded)
  myRobot.Run()
